package com.shuoyue.ycgk.ui.recruitment.newsinfo;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.FileUtil;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.NoticeFile;
import com.shuoyue.ycgk.entity.RecruitmentCommentCourse;
import com.shuoyue.ycgk.entity.RecruitmentItem;
import com.shuoyue.ycgk.ui.common.download.DownloadContract;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.ui.news.NoticeFileAdapter;
import com.shuoyue.ycgk.ui.recruitment.RecommentCourseAdapter;
import com.shuoyue.ycgk.utils.MyRichTextHelper;
import com.shuoyue.ycgk.utils.NOpenFiles;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.zzhoujay.richtext.RichText;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 公告详情
 */
public class FragmentNoticeDetail extends BaseMvpFragment implements DownloadContract.View {
    @BindView(R.id.personNum)
    TextView personNum;
    @BindView(R.id.jobNum)
    TextView jobNum;
    @BindView(R.id.status)
    TextView status;
    @BindView(R.id.reportTime)
    TextView reportTime;
    @BindView(R.id.penTestTime)
    TextView penTestTime;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.lay_recommendCourse)
    LinearLayout layRecommendCourse;
    @BindView(R.id.recycleViewCourse)
    RecyclerView recycleViewCourse;
    @BindView(R.id.expand_all)
    TextView expandAll;
    @BindView(R.id.regLayout)
    LinearLayout regLayout;
    @BindView(R.id.penLayout)
    LinearLayout penLayout;

    RecruitmentItem info;
    RecommentCourseAdapter courseAdapter;
    @BindView(R.id.filename)
    RecyclerView filename;
    NoticeFileAdapter mAdapter;
    DownloadContract.Presenter downloadPresenter;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;

    public static FragmentNoticeDetail getInstance(RecruitmentItem recruitmentItem) {
        FragmentNoticeDetail fragment = new FragmentNoticeDetail();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", recruitmentItem);

        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void initParams(Bundle bundle) {
        info = (RecruitmentItem) bundle.getSerializable("data");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news_info_detail;
    }

    @Override
    protected void initView() {
        recycleViewCourse.setLayoutManager(new LinearLayoutManager(mContext));
        recycleViewCourse.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
        courseAdapter = new RecommentCourseAdapter(info.getRecommendProductDTOS());
        recycleViewCourse.setAdapter(courseAdapter);

        personNum.setText(String.valueOf(info.getEnrollment()));
        jobNum.setText(String.valueOf(info.getPostNum()));
        regLayout.setVisibility(TextUtils.isEmpty(info.getRegStartTime())
                || TextUtils.isEmpty(info.getRegEndTime()) ? View.GONE : View.VISIBLE);
        penLayout.setVisibility(TextUtils.isEmpty(info.getExamStartTime())
                || TextUtils.isEmpty(info.getExamEndTime()) ? View.GONE : View.VISIBLE);
        reportTime.setText(info.getRegStartTime() + "至" + info.getRegEndTime());
        penTestTime.setText(info.getExamStartTime() + "至" + info.getExamEndTime());
        MyRichTextHelper.showRich(mContext, info.getDescription(), content);

        courseAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            RecruitmentCommentCourse commentCourse = (RecruitmentCommentCourse) adapter.getItem(position);
            startActivity(new Intent(mContext, CourseInfoActivity.class)
                    .putExtra("id", commentCourse.getProductId()));
        });
        layRecommendCourse.setVisibility(info.getRecommendProductDTOS() == null || info.getRecommendProductDTOS().size() == 0
                ? View.GONE : View.VISIBLE);
        if (info.getStatus() == 1) {
            status.setText("即将报名");
            status.setBackgroundResource(R.mipmap.tag_in_blue);
        } else if (info.getStatus() == 0) {
            status.setText("正在报名");
            status.setBackgroundResource(R.mipmap.tag_in_orange);
        } else {
            status.setText("报名结束");
            status.setBackgroundResource(R.mipmap.tag_in_gray);
        }
        //附件

        mAdapter = new NoticeFileAdapter(info.getAttachmentList());
        filename.setAdapter(mAdapter);
        filename.setLayoutManager(new LinearLayoutManager(mContext));
        filename.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 4), false));
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            NoticeFile noticeFile = mAdapter.getItem(position);
            switch (view.getId()) {
                case R.id.tv_title:
                    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    if (EasyPermissions.hasPermissions(mContext, permissions)) {
                        File dir = new File(FileUtil.getPath(mActivity) + "/公告/");
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }

                        File file = new File(FileUtil.getPath(mActivity) + "/公告/", noticeFile.getAttachmentName());
                        if (file.exists()) {
                            NOpenFiles.openFile(mActivity, file);
                        } else {
                            downloadPresenter.downloadFile(noticeFile.getAttachmentUrl(),
                                    new File(FileUtil.getPath(mActivity) + "/公告/", noticeFile.getAttachmentName()).getAbsolutePath());
                        }

                    } else {
                        EasyPermissions.requestPermissions(mActivity,
                                "需要用到文件访问权限", 1, permissions);
                    }

                    break;
            }
        });
        registScrollToTopAndBottom(scrollView);
    }

    @Override
    protected void initData() {
        super.initData();
        downloadPresenter = new DownloadContract.Presenter();
        downloadPresenter.attachView(this);

    }

    @Override
    public String getTitle() {
        return "公告详情";
    }

    @OnClick({R.id.expand_all, R.id.bottom, R.id.top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.expand_all:
                content.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                expandAll.setVisibility(View.GONE);
                break;
            case R.id.bottom:
                scrollView.fullScroll(View.FOCUS_DOWN);//滚到底部

                break;
            case R.id.top:
                scrollView.fullScroll(View.FOCUS_UP);//

                break;
        }
    }

    @Override
    public void downloadSuc(File f) {
        NOpenFiles.openFile(mActivity, f);
    }

    @Override
    public void progress(int progress) {

    }

    @Override
    public void downloadStart() {

    }

    @Override
    public void downloadError(String msg) {

    }

}
